package servlet;

import bean.News;
import bean.NewsofUser;
import bean.User;
import dao.NewsDao;
import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet("/userMan.do")
public class UserManServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public UserManServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
        String updateid = request.getParameter("updateid");//修改
        String deleteid = request.getParameter("deleteid");//删除
        String select ="";
        select += request.getParameter("select");//查询

        System.out.println("updateid="+updateid);
        System.out.println("deleteid="+deleteid);
        System.out.println("select="+select);
        if(updateid!=null){
            User user = UserDao.update(Integer.parseInt(updateid));
            System.out.println("user="+user.toString());
            request.getSession().setAttribute("userupdate", user);
            response.sendRedirect("./jsp/UserMan.jsp");
//            response.sendRedirect("/press_release_system_war_exploded/userlimit"); // Redirect to welcome page

            return;
        }
        if(deleteid!=null){
            UserDao.Delete(Integer.parseInt(deleteid));
            List<User> userlist =  UserDao.select();
            //request.setAttribute("list", list);
            request.getSession().setAttribute("userlist", userlist);
//            response.sendRedirect("./jsp/UserMan.jsp");
            response.sendRedirect("/press_release_system_war_exploded/userlimit"); // Redirect to welcome page

            return;

        }
        if(!select.isEmpty()){
            System.out.println("select.isEmpty()");
            List<User> userlist =  UserDao.select(select);
            //request.setAttribute("list", list);
            request.getSession().setAttribute("userlist", userlist);
//            response.sendRedirect("/press_release_system_war_exploded/userlimit"); // Redirect to welcome page
            response.sendRedirect("/press_release_system_war_exploded/userselectlimit"); // Redirect to welcome page

//            response.sendRedirect("./jsp/UserMan.jsp");
//            System.out.println("没有select");
            System.out.println(select+"111");
            return;
        }
        response.sendRedirect("/press_release_system_war_exploded/userlimit"); // Redirect to welcome page

//        response.sendRedirect("./jsp/News.jsp");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
